home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / CMPLTPAS / AVERAGER.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-24  |  2KB  |  49 lines

  1. {--------------------------------------------------------------}
  2. {                           Averager                           }
  3. {                                                              }
  4. {             Binary file I/O demonstration program            }
  5. {                                                              }
  6. {                             by Jeff Duntemann                }
  7. {                             Turbo Pascal V5.0                }
  8. {                             Last update 7/24/88              }
  9. {                                                              }
  10. {     From: COMPLETE TURBO PASCAL 5.0  by Jeff Duntemann       }
  11. {    Scott, Foresman & Co., Inc. 1988   ISBN 0-673-38355-5     }
  12. {--------------------------------------------------------------}
  13.  
  14. PROGRAM Averager;
  15.  
  16. VAR
  17.   IntFile       : FILE OF Integer;
  18.   I,J,Count     : Integer;
  19.   Average,Total : Real;
  20.  
  21. BEGIN
  22.   Assign(IntFile,'INTEGERS.BIN');
  23.   {$I-} Reset(IntFile); {$I+}
  24.   I := IOResult;
  25.   IF I <> 0 THEN
  26.     BEGIN
  27.       Writeln('>>File INTEGERS.BIN is missing or damaged.');
  28.       Writeln('  Please investigate and run the program again.')
  29.     END
  30.   ELSE
  31.     BEGIN
  32.       Count := 0; Total := 0.0;
  33.       WHILE NOT EOF(IntFile) DO
  34.         BEGIN
  35.           Read(IntFile,J);
  36.           IF NOT EOF(IntFile) THEN
  37.             BEGIN
  38.               Count := Count + 1;
  39.               Total := Total + J
  40.             END;
  41.         END;
  42.       Close(IntFile);
  43.       AVERAGE := Total / Count;
  44.       Writeln;
  45.       Writeln('>>There are ',Count,' integers in INTEGERS.BIN.');
  46.       Writeln('  Their average value is ',Average:10:6,'.');
  47.     END
  48. END.
  49.